package org.example.servicestudent.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.model.manage.bean.Result;
import org.example.model.manage.bean.po.Student;
import org.example.model.student.bean.Task;
import org.example.servicestudent.service.StudentService;
import org.example.servicestudent.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private TaskService taskService;

    @PostMapping("/binding")
    public Result binding(Long studentId, String unionId, String openId) {
        Student stu = studentService.binding(studentId, unionId, openId);
        if(stu != null)  return Result.success(stu);
        return Result.error(10008, "不存在该学号");
    }

    @GetMapping("/info")
    public Result info(String unionId) {
        Student stu = studentService.getOne(new QueryWrapper<Student>().eq("union_id", unionId));
        if(stu != null)  return Result.success(stu);
        return Result.error(10007, "未绑定");
    }

    @PostMapping("/positioning")
    public Result positioning(String unionId, Double latitude, Double longitude) {
        if(studentService.positioning(unionId, latitude, longitude)) return Result.success("success");
        return Result.error(10001, "不在范围内");
    }

    @GetMapping("/task")
    public Result task(String unionId) {
        Integer majorId = studentService.getOne(new QueryWrapper<Student>().eq("union_id", unionId)).getMajorId();
        if(majorId == null)  return Result.error(10007, "未绑定");
        List<Task> list = taskService.getList(majorId);
        return Result.success(list);
    }
}
